import { routerRedux } from 'dva/router';
import {listHisVersion,saveNewVersion,delectApk } from '../services/appVersion';

export default {
  namespace: 'appVersion',
  state: {
    loading: true,
    appVersionData: {
      errorCode:0,
      msg:"",
      data:{
        pageNo:null,
        pageCount:null,
        total:null,
        pageSize:null,
        latestTime:null,
        list:[]
      },
    },
  },

  effects: {
    *listHisVersion({ payload,callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(listHisVersion, payload);
      yield put({
        type: 'listHisVersionSuccess',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
    *saveNewVersion({ payload,callback }, { call, put }) {
      const response = yield call(saveNewVersion, payload);
      if (callback) callback(response);
    },
    *delectApk({ payload,callback }, { call, put }) {
      const response = yield call(delectApk, payload);
      if (callback) callback(response);
    },
    *turnToAddVersion({ payload }, { put }) {
      yield put(routerRedux.push('/versionUpgrade/createNewVersion'));
    },
    *turnToVersionList({ payload }, { put }) {
      yield put(routerRedux.push('/versionUpgrade/hisVersion'));
    },
  },

  reducers: {
    listHisVersionSuccess(state, action) {
      return {
        ...state,
        appVersionData: action.payload,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
  },
};
